<?php
/*
 * Create On 2016-12-18
 * Athor  By Zhang Long
 * Email  By dwzhanglong@126.com
 * Func	  会员中心-修改密码
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('user');
$itable = Table('user_info');

if(IS_POST)
{
	$money = intval($_POST['money']);
	($money < 1000) && ShowMsg('充值金额不能低于1000',U($ac.'/'.$op));
	
	$payment = $_POST['payment'];
	!in_array($payment,['alipay','weixin','qq','zhb']) && $payment = 'alipay';
	
	$ins = [];
	$ins['uid']		=	$_uid;
	$ins['payment']	=	$payment;
	$ins['money']	=	$money;
	$ins['award']	=	0;	//	奖励
	$ins['status']	=	0;
	$ins['ip']		=	$_Y['ip'];
	$ins['dateline']=	TIMESTAMP;
	$id = $db->add('user_money_recharge',$ins);
	!$id && ShowMsg('充值失败，请重新充值',U($ac.'/'.$op.'/'.$do));
	
	//	配置文件
	include_once(LIB_PATH.'payment/dinpay/merchant.php');
	//	订单信息
	$params = [];
	$params['merchant_code']	=	$merchant_code;	//	商户号
	$params['service_type']		=	$payment.'_scan';
	$params['notify_url']		=	URL.'lib/payment/dinpay/notify.php';
	$params['interface_version']=	'V3.1';
	$params['client_ip']		=	$_Y['ip'];
	$params['sign_type']		=	'RSA-S';
	$params['order_no']			=	'CP'.date('YmdHis').$id;
	$params['order_time']		=	TIMESTAMP;
	$params['order_amount']		=	$money;
	$params['product_name']		=	'在线充值';
	//$params['product_code']		=	'';
	//$params['product_num']		=	'';
	//$params['product_desc']		=	'';
	$params['extra_return_param']=	$id;	//	回传参数
	//$params['extend_param']		=	'';	//	扩展参数
	
	ksort($params);
	
	$sign = [];
	foreach($params as $key => $val)
	{
		$sign[] = $key.'='.$val;
	}
	$sign = implode('&',$sign);
	
	//	商户私钥
	$merchant_private_key= openssl_get_privatekey($merchant_private_key);
	openssl_sign($sign,$sign_info,$merchant_private_key,OPENSSL_ALGO_MD5);	
	$params['sign'] = base64_encode($sign_info);
	
	//	提交到网关
	$ch = curl_init();	
	curl_setopt($ch,CURLOPT_URL,"https://api.dinpay.com/gateway/api/scanpay");
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);	
	$response=curl_exec($ch);
	curl_close($ch);
	
	//	解析结果、提取二维码内容
	$res = simplexml_load_string($response);
	
	exit();
}

if($do == 'log')
{
	$statuss = ['未支付','已支付'];
	
	$data  = [];
	$where = ' FROM '.Table('user_money_recharge').' t WHERE t.uid = '.$_uid;
	
	$where .= ' AND t.status = 1';

	$status = intval($_GET['status']);
	$status && $where .= ' AND t.status = '.($status > 0 ? $status : 0);

	$dt1 = NoBadStr($_GET['dt1']);
	$dt1 && $where .= ' AND t.dateline > '.strtotime($dt1);
	$dt2 = NoBadStr($_GET['dt2']);
	$dt2 && $where .= ' AND t.dateline < '.strtotime($dt2);

	$tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
	if($tmp['p'])
	{
		$page = max(1,intval($_GET['page']));
		$size = 20;
		$limit    = ' LIMIT '.($page - 1) * $size.','.$size;
		
		$orderby = ' ORDER BY t.id DESC ';
		$url = U($ac.'/'.$op,['dt1'=>$dt1,'dt2'=>$dt2]);
		
		$data = $db->getall('SELECT t.* '.$where.$orderby.$limit);
		
		$multi = Multi($url,$page,$tmp['p'],$size);
	}
	
	$tpl->display('member/recharge_log');
	exit;
}

$tpl->display('member/recharge');